package com.zsr.controller;

import com.zsr.pojo.Book;
import com.zsr.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zsr
 * @Desc
 * @Date 2021/5/3 15:22
 */
@Controller
@RequestMapping("/book")
public class BookController {
    //controller 调用service
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    //查询所有书籍
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Book> books = bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }
    //获取书籍（修改前先回去获取）
    @RequestMapping("/getBook")
    public String getBook(int id,Model model){
        System.out.println(id);
        Book book = bookService.queryBookById(id);
        System.out.println(book);
        model.addAttribute("editBook",book);
        return "editBook";
    }

    //添加和更新书籍
    @RequestMapping("/toEditBook")
    public String toEditBookPage(){
        return "editBook";
    }
    @PostMapping("/editBook")
    public String editBook(String action, Book book){
        System.out.println(action);
        if(action.equals("add")){
            bookService.addBook(book);
        }else{
            bookService.updateBook(book);
        }
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }


}
